n, k = map(int, input().split())
inf = float('inf')
dp = [inf] * n
dp[0] = 0

while True:
    flag = True
    for item in (1, k):
        for i in range(n):
            if dp[(i - item) % n] + 1 < dp[i]:
                dp[i] = dp[(i - item) % n] + 1
                flag = False
    if flag:
        break
print(max(dp))
